﻿using System;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Support;
using Microsoft.SharePoint.Utilities;
using System.Web;

namespace MS.InformationManagement.Records.Receivers
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class DocumentTypeReceiver : SPItemEventReceiver
    {

        /// <summary>
        /// An item is being deleted.
        /// </summary>
        public override void ItemDeleting(SPItemEventProperties properties)
        {
            base.ItemDeleting(properties);
            if (properties.List.Title.Equals("Tipos Documentales"))
            {
                try
                {
                    this.EventFiringEnabled = false;
                    properties.Cancel = true;
                    properties.ErrorMessage = string.Format("No es posible eliminar items de esta lista", properties.Web.CurrentUser.LoginName);
                }
                catch (Exception ex)
                {
                    Logger.LogException(ex, Microsoft.SharePoint.Administration.EventSeverity.Error,
                         SPDiagnosticsAreas.SHAREPOINTFOUNDATION, SPDiagnosticsAreas.SharepointFoundationDiagnosticsCategories.WEBPARTS);
                    throw;
                }
                finally { this.EventFiringEnabled = true; }
            }
        }

       /// <summary>
       /// An item is being added.
       /// </summary>
       public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);
           if (properties.List.Title.Equals("Tipos Documentales"))
           {
               try
               {
                   this.EventFiringEnabled = false;
                   string fileName = properties.BeforeUrl;
                   string extension = fileName.Substring(fileName.LastIndexOf('.') + 1);
                   switch (extension)
                   {
                       case GlobalConstants.WORD:
                       case GlobalConstants.WORD_LEGACY:
                       case GlobalConstants.EXCEL:
                       case GlobalConstants.EXCEL_LEGACY:
                       case GlobalConstants.POWERPOINT:
                       case GlobalConstants.POWERPOINT_LEGACY:
                           break;
                       default:
                           properties.Cancel = true;
                           properties.Status = SPEventReceiverStatus.CancelWithError;
                           properties.ErrorMessage = "El tipo de archivo que intenta cargar no es un archivo de plantilla valido. Verifique que el archivo sea un archivo de Microsoft Office.";
                           break;
                   }
               }
               catch (Exception ex)
               {
                   Logger.LogException(ex, Microsoft.SharePoint.Administration.EventSeverity.Error,
                        SPDiagnosticsAreas.SHAREPOINTFOUNDATION, SPDiagnosticsAreas.SharepointFoundationDiagnosticsCategories.WEBPARTS);
                   throw;
               }
               finally { this.EventFiringEnabled = true; }
           }
       }


    }
}
